home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / pcscheme / geneva / sources.exe / SOURCES / ASM / MEMMGR.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-01-27  |  6.4 KB  |  276 lines

  1. ;* MEMMGR.ASM
  2. ;************************************************************************
  3. ;*                                    *
  4. ;*        PC Scheme/Geneva 4.00 Borland TASM code            *
  5. ;*                                    *
  6. ;* (c) 1985-1988 by Texas Instruments, Inc. See COPYRIGHT.TXT        *
  7. ;* (c) 1992 by L. Bartholdi & M. Vuilleumier, University of Geneva    *
  8. ;*                                    *
  9. ;*----------------------------------------------------------------------*
  10. ;*                                    *
  11. ;*        Initialise and support the paging system        *
  12. ;*                                    *
  13. ;*----------------------------------------------------------------------*
  14. ;*                                    *
  15. ;* Created by: John Jensen        Date: 1985            *
  16. ;* Revision history:                            *
  17. ;* - 18 Jun 92:    Renaissance (Borland Compilers, ...)            *
  18. ;*                                    *
  19. ;*                    ``In nomine omnipotentii dei''    *
  20. ;************************************************************************
  21. IDEAL
  22. %PAGESIZE    60, 132
  23. MODEL    medium
  24. LOCALS    @@
  25.  
  26.     INCLUDE    "scheme.ash"
  27.  
  28. DATASEG
  29.     EXTRN    _top:word
  30.  
  31. paragraphnum    DW    0         ; # of paragraphs of memory available
  32. firstparagraph    DW    0         ; first actually used para. for Scheme heap
  33. first_dos    DW    0         ; first DOS para. for Scheme heap
  34. emshandle    DW    0ffffh
  35. emspages    DB    0
  36.  
  37. UDATASEG
  38. emsframe    DW    ?
  39. emsbias        DB    ?
  40. defpagesize    DW    ?
  41.  
  42. CODESEG
  43. ;======================================================================
  44. ;
  45. ;    Get page base address of page
  46. ;
  47. ; On exit, carry is clear to indicate page is always in memory
  48. ; (for compatibility with extended and expanded versions of this routine)
  49. ;
  50. ;======================================================================
  51.  
  52. PROC C    getbase, @@base:word
  53.     mov    bx, [@@base]
  54.     mov    ax, [pagetable+bx]    ; Get table indicator
  55.     clc                ; always succeeds
  56.     ret
  57. ENDP
  58.  
  59. ;======================================================================
  60. ;
  61. ; InitMem()
  62. ;    Compute the best page size, but not smaller than MIN_PAGESIZE
  63. ;
  64. ;======================================================================
  65. PROC C    initmem    USES si di
  66.     LOCAL    @@paraspepage, @@freepages
  67.  
  68.     push    ds            ;Assume es = ds
  69.     pop    es
  70.     
  71.     action    <Seeking for free memory',13,10,'>
  72.     mov    bx, 0ffffh         ; first ask for too much
  73.     mov    ah, 048h
  74.     int    MSDOS             ; DOS gets an error, but tells us
  75.                     ;   in bx how much we CAN get
  76.     action    <Allocating conventional memory',13,10,'>
  77.     mov    [paragraphnum], bx
  78.     mov    ah, 048h
  79.     int    MSDOS             ; reissue allocation request
  80.     mov    [first_dos], ax     ; save address for returning it to DOS
  81.     mov    [firstparagraph], ax     ; save address for Scheme heap
  82.  
  83.     action    <Trying to allocate expanded memory',13,10,'>
  84.     mov    ax, 3500h or EMMINT
  85.     int    MSDOS
  86.     mov    di, 10
  87. DATASEG
  88. @@emmname    DB    'EMMXXXX0'
  89. EMMLENGTH = $ - @@emmname
  90. CODESEG
  91.     lea    si, [@@emmname]
  92.     mov    cx, EMMLENGTH
  93.     repe    cmpsb
  94.     jne    @@noems
  95.     mov    ah, 40h
  96.     int    EMMINT
  97.     or    ah, ah
  98.     jnz    @@noems
  99.     mov    ah, 41h
  100.     int    EMMINT
  101.     or    ah, ah
  102.     jnz    @@noems
  103.     mov    [emsframe], bx
  104.     mov    ah, 42h
  105.     int    EMMINT
  106.     or    ah, ah
  107.     jnz    @@noems
  108.     cmp    bx, MAXEMS
  109.     jb    @@takeallofem
  110.     mov    bx, MAXEMS
  111. @@takeallofem:
  112.     action    <Allocating EMS... >
  113.     mov    [emspages], bl
  114.     mov    ah, 43h
  115.     int    EMMINT
  116.     or    ah, ah
  117.     jnz    @@noems
  118.     mov    [emshandle], dx
  119.     action    <EMS memory allocated',13,10,'>
  120.  
  121. @@noems:
  122.     action    <Compute best page size',13,10,'>
  123.     mov    ax, [firstparagraph]
  124.     add    ax, [paragraphnum]     ; max number of paragraphs
  125.     xchg    ax, [paragraphnum]
  126.     xor    dx, dx             ; get ready for divide
  127.     mov    cx, NUMPAGES-PREALLOC
  128.     sub    cl, [emspages]        ; cx <= number heap allocated pages
  129.     mov    [@@freepages], cx
  130.     div    cx             ; ax <= paras-per-page
  131.  
  132.     cmp    ax, MIN_PAGESIZE shr 4
  133.     jge    @@longpages
  134.     mov    ax, MIN_PAGESIZE shr 4
  135. @@longpages:
  136.     cmp    ax, 800h
  137.     jb    @@nottoobig
  138.     mov    ax, 7ffh        ; when page sizes become negative...
  139. @@nottoobig:        
  140.     mov    [@@paraspepage], ax
  141.     mov    cx, 4            ; compute page size
  142.     shl    ax, cl
  143.     mov    [defpagesize], ax
  144.     mov    si, ax
  145.  
  146.     action    <Initialize page management table... >
  147.     xor    cx, cx             ; number of pages
  148.     mov    dx, [nextpage]
  149.     mov    [freepage], dx
  150.     mov    ax, [firstparagraph]     ; ax <= next paragraph
  151.     mov    di, [paragraphnum]     ; di <= (paragraphnum - paras per page)
  152.     sub    di, [@@paraspepage]
  153. @@loop:
  154.     cmp    di, ax             ; Did we reach it ?
  155.     jb    @@done
  156.     cmp    cx, [@@freepages]    ; See if we have filled the table
  157.     jae    @@done
  158.     mov    bx, dx
  159.     shl    bx, 1
  160.     mov    [pagetable+bx], ax
  161.     mov    [psize+bx], si
  162.     and    [attrib+bx], not NOMEMORY
  163.     inc    dx
  164.     mov    [pagelink+bx], dx
  165.     mov    [nextcell+bx], 0
  166.     inc    cx             ; one more page done
  167.     add    ax, [@@paraspepage]    ; next block
  168.     jmp    @@loop
  169. @@done:
  170.     mov    al, [emspages]
  171.     mov    ah, dl
  172.     shl    ah, 1
  173.     sbb    ah, 0            ; in case we overflowed to 0,
  174.     mov    [emsbias], ah        ; put 0xff instead
  175. @@ems:
  176.     cmp    al, 0
  177.     je    @@emsdone
  178.     cmp    dx, NUMPAGES
  179.     je    @@emsdone
  180.     action    <EMS... >
  181.     mov    bx, dx
  182.     shl    bx, 1
  183.     mov    [pagetable+bx], EMSPAGE
  184.     mov    [psize+bx], EMSSIZE
  185.     and    [attrib+bx], not NOMEMORY
  186.     inc    dx
  187.     mov    [pagelink+bx], dx
  188.     mov    [nextcell+bx], 0
  189.     inc    cx             ; one more page done
  190.     dec    al
  191.     jmp    @@ems
  192. @@emsdone:
  193.     action    <',13,10,'Exiting memory allocation subroutine',13,10,'>
  194.     sub    [emspages], al        ; we'll waste these
  195.     mov    [nextpage], dx         ; nextpage = lastpage
  196.     mov    [lastpage], dx
  197.     mov    ax, cx
  198.     ret
  199. ENDP    initmem
  200.  
  201. MACRO    bubble    from, to
  202.     LOCAL    @@skip
  203.     cmp    bl, [ss:@@frame+from]
  204.     jne    @@skip
  205.     xchg    bl, [ss:@@frame+to]
  206.     xchg    [ss:@@frame+from], bl
  207. @@skip:
  208. ENDM
  209.  
  210. MACRO    copy    from, to
  211.     mov    al, [ss:@@frame+from]
  212.     mov    [ss:@@frame+to], al
  213. ENDM
  214.  
  215. PROC    loadems
  216. DATASEG
  217. @@frame    db    4 dup (0ffh)
  218. CODESEG
  219.     ; our purpose is to map page bx/2 to some place in expanded memory,
  220.     ; returning a valid segment address in [pagetable+bx]
  221.     ;NOTE: we may not assume anything about ds ! all addressing must be
  222.     ;done relative to ss ('small data' model)
  223.     push    ax
  224.     mov    ax, [ss:pagetable+bx]
  225.     cmp    ax, EMSPAGE
  226.     je    @@newpage
  227.     bubble    3, 2
  228.     bubble    2, 1
  229.     bubble    1, 0
  230.     pop    ax
  231.     ret
  232. @@newpage:
  233.     push    bx
  234.     mov    bl, 4
  235. @@loop:
  236.     cmp    [ss:@@frame+bx-1], 0ffh
  237.     jne    @@found
  238.     dec    bx
  239.     jnz    @@loop
  240. @@found:
  241.     cmp    bl, 4
  242.     jne    @@usebl            ; now we gotta discard the oldest page
  243.     mov    bl, [ss:@@frame+3]
  244.     mov    ax, EMSPAGE
  245.     xchg    ax, [ss:pagetable+bx]
  246.     sub    ax, [ss:emsframe]
  247.     shr    ax, 1
  248.     shr    ax, 1
  249.     mov    bl, ah
  250. @@usebl:                ; let's work on physical page bl
  251.     mov    al, bl
  252.     pop    bx            ; get the page to load
  253.     push    bx dx ax
  254.     mov    ah, 44h            ; EMS map pages
  255.     sub    bl, [ss:emsbias]
  256.     shr    bx, 1
  257.     mov    dx, [ss:emshandle]
  258.     int    EMMINT
  259.     pop    ax dx bx        ; this is the physical page #
  260.     mov    ah, al
  261.     xor    al, al
  262.     shl    ax, 1
  263.     shl    ax, 1
  264.     add    ax, [ss:emsframe]
  265.     mov    [ss:pagetable+bx], ax    ; now write this value, so it can
  266.                     ; be found immediately next time
  267.     copy    2, 3
  268.     copy    1, 2
  269.     copy    0, 1
  270.     mov    [ss:@@frame+0], bl
  271.     pop    ax
  272.     ret
  273. ENDP    loadems
  274.  
  275.     END
  276.